from concurrent.futures import ThreadPoolExecutor,as_completed,ProcessPoolExecutor

import time

# 耗cpu操作
def fibonacci(n):
    if n<=2:
        return 1
    return fibonacci(n-1)+fibonacci(n-2)

def random_sleep(n):
    time.sleep(n)
    return n
def ThreadFib(n):
    with ThreadPoolExecutor() as executor:
        start = time.time()
        # all_task=[executor.submit(fibonacci,(num)) for num in range(25,35)]
        all_task=[executor.submit(random_sleep,(num)) for num in [2]*30]
        for future in as_completed(all_task):
            data = future.result()
            print("exec result:{}".format(data))
        print("cost time is {}".format(time.time() - start))


def ProcessFib(n):
    with ProcessPoolExecutor() as executor:
        start = time.time()
        # all_task=[executor.submit(fibonacci,(num)) for num in range(25,35)]
        all_task=[executor.submit(random_sleep,(num)) for num in [2]*30]
        for future in as_completed(all_task):
            data = future.result()
            print("exec result:{}".format(data))
        print("cost time is {}".format(time.time() - start))




if __name__ == '__main__':
    # ThreadFib(5)
    ProcessFib(5)